<template>
  <div class="record">
    <div class="title">
    </div>
    <div class="content">
      <el-form :model="ruleForm"
               ref="numberValidateForm"
               label-width="100px"
               class="demo-ruleForm"
               :rules="rules"
      >
        <el-row>
          <el-col :span="12">
            <span>日期:</span>
            <el-date-picker
                v-model="ruleForm.date"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择日期">
            </el-date-picker>
          </el-col>
          <el-col :span="12">
            <span>姓名:</span>
            <el-input v-model="ruleForm.name" placeholder="请输入姓名">

            </el-input>
          </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item
                        prop="huji"
                        label="户籍地:"
                >
                    <el-input v-model="ruleForm.huji" placeholder="请输入户籍"></el-input>
                </el-form-item>

            </el-col>
          <el-col :span="12">
            <el-form-item
                prop="address"
                label="居住地址:"
            >
              <el-input v-model="ruleForm.address" placeholder="请输入地址"></el-input>
            </el-form-item>

          </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item
                        prop="age"
                        label="年龄:"
                >
                    <el-input v-model="ruleForm.age" placeholder="请输入年纪"></el-input>
                </el-form-item>

            </el-col>
          <el-col :span="12">
            <el-form-item
                prop="identity"
                label="身份证:"
            >
              <el-input v-model="ruleForm.identity" placeholder="请输入身份证"></el-input>
            </el-form-item>

          </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <el-form-item
                        prop="phone"
                        label="手机号码:"
                >
                    <el-input v-model="ruleForm.phone" placeholder="请输入手机号码"></el-input>
                </el-form-item>

            </el-col>
          <el-col :span="12">
            <span>孕产次:</span>
            <el-input v-model="ruleForm.ycc" placeholder="请输入孕产次"></el-input>
          </el-col>

        </el-row>
        <el-row>
            <el-col :span="12">
                <span>末次月经:</span>
                <el-date-picker
                        v-model="ruleForm.mcyj"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择末次月经">
                </el-date-picker>
            </el-col>
          <el-col :span="12">
            <span>预产期:</span>
            <el-date-picker
                v-model="ruleForm.ycq"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择预产期">
            </el-date-picker>
          </el-col>

        </el-row>
        <el-row>
            <el-col :span="12">
                <span>孕周:</span>
                <el-input v-model="ruleForm.yz" placeholder="请输入孕周"></el-input>
            </el-col>
          <el-col :span="12">
            <span>高危因素:</span>
            <el-input v-model="ruleForm.gwys" placeholder="请输入高危因素"></el-input>
          </el-col>

        </el-row>
        <el-row>
            <el-col :span="12">
                <span>下次产检时间:</span>
                <el-date-picker
                        v-model="ruleForm.xccj"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择下次产检时间">
                </el-date-picker>
            </el-col>
          <el-col :span="12">
            <span>评分:</span>
            <el-input v-model="ruleForm.rate" placeholder="请输入评分"></el-input>
          </el-col>

        </el-row>
        <el-row>
            <el-col :span="12">
                <span>评分颜色:</span>
                <el-select v-model="rateColor" placeholder="请选择" multiple>
                    <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-col>
          <el-col :span="12">
            <span>分娩状态:</span>
            <el-select v-model="fmStatus" placeholder="请选择分娩状态">
              <el-option
                  v-for="item in options1"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-col>
        </el-row>
        <div class="btn" >
          <span @click="_createUser">保存</span>
        </div>
      </el-form>
    </div>
  </div>
</template>


<script>
import {
        identity,
        phone,
        }
        from "../../utils/validate";
import {createUser, updateUser} from "../../api/users";
import {EventBus} from "../../utils/event";

export default {
  name: "index",
  data() {
    const checkIdentity = (rule, value, callback) =>  {
      if (!identity.test(value)) {
        return callback(new Error("身份证录入错误,请重新录入"))
      }
    };
    const checkPhone = (rule, value, callback) => {
      if (!phone.test(value)) {
        return callback(new Error("手机号码格式错误,请重新录入"))
      }
    }
    return {
      rules: {
        age: [
          // {type: 'number', message: '年龄必须为数字值'},
        ],
        // address: [
        //   {required: true, message: '地址不能为空'},
        // ],
        identity:[
          {validator:checkIdentity,trigger: 'blur'},
          {required: true, message: '身份证不能为空'},
        ],
        phone:[
          {validator:checkPhone,trigger: 'blur'},
          {required: true, message: '手机号码不能为空'},
        ]
      },
      status:"create",
      ruleForm: {
        name: "",//姓名
        age: "",//年龄
        huji: "",//户籍
        address: "",//地址
        identity: "",//身份证
        phone: "",//手机号码
        ycc: "",//孕产次
        mcyj: null,//末次月经
        date: null,//日期
        ycq: null,//预产期
        yz: "",//孕周
        gwys: "",//高危因素
        rate: "",//评分
        xccj: null,//下次产检时间
      },
      options: [{
        value: '绿',
        label: '绿'
      }, {
        value: '黄',
        label: '黄'
      }, {
        value: '红',
        label: '红'
      }, {
        value: '橙',
        label: '橙'
      }, {
        value: '紫',
        label: '紫'
      }],
      rateColor: "",//评分颜色
      options1:[
        {
          value: 1,
          label: '已分娩'
        },
        {
          value: 0,
          label: '未分娩'
        }
      ],
      fmStatus:""
    }
  },
  created() {
    EventBus.$on("user",(obj) => {
      this.ruleForm.name = obj.name;
      this.ruleForm.age = obj.age ? obj.age : '';
      this.ruleForm.address = obj.address;
      this.ruleForm.identity = obj.identity;
      this.ruleForm.phone = obj.phone;
      this.ruleForm.ycc = obj.ycc;
      this.ruleForm.mcyj = obj.mcyj;
      this.ruleForm.date = obj.date;
      this.ruleForm.ycq = obj.ycq;
      this.ruleForm.yz = obj.yz;
      this.ruleForm.gwys = obj.gwys;
      this.ruleForm.rate = obj.rate;
      this.ruleForm.xccj = obj.xccj;
      this.rateColor = obj.rateColor ? obj.rateColor.split(",") : "";
      this.fmStatus = obj.fmStatus;
      this.status = "modify"
      this.id = obj.id;
      this.ruleForm.huji = obj.huji;
    })
    },
  beforeDestroy() {
    EventBus.$off("user")
    },
  methods:{
    async _createUser(){
      let args = {fmStatus:this.fmStatus,rateColor:this.rateColor ?this.rateColor.join(",") : ""}
      let postForm = Object.assign(this.ruleForm,args)
      // if (Object.values(postForm).includes("")) {
      //   console.log(postForm)
      //   this.$message.warning("请把信息填写完整")
      //   return
      // }
      if (this.status === 'create') {
        let res = await createUser(postForm);
      }else {
        let res = await updateUser(postForm, this.id);
      }

      this.$message.success("保存成功")
      this.$router.push({path:"/"});

    }
  }
}
</script>

<style scoped>
.record {
  width: 100%;
  height: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
}

.content {
  width: 800px;
  height: 600px;
}

.el-input {
  margin-top: 10px;
  width: 300px;
}

.rate {
  display: flex;
}


span {
  margin-right: 3px;
  width: 90px;
  display: inline-block;
  text-align: right;
}

.btn span {
  margin-top: 20px;
  width: 100px;
  height: 40px;
  background-color: #a1c25a;
  line-height: 40px;
  text-align: center;
  transition: .3s ease-in-out;
  cursor: pointer;
  color:#053919;
  background-image: linear-gradient(to bottom,#bddb7e 0,#85a936 100%);
}


.el-date-editor {
  width: 300px !important;
}


</style>